package com.wht.bill.fragment;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.wht.bill.R;
import com.wht.bill.activity.AddBankActivity;
import com.wht.bill.adapter.AccountAdapter;
import com.wht.bill.bean.AccountBean;
import com.wht.bill.bean.BillBean;
import com.wht.bill.utils.CacheUtils;
import com.wht.bill.utils.NumberUtils;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 记账本--我的账户
 */
public class MenuMyFragment extends BaseFragment {

    @BindView(R.id.cash_surplus)
    TextView cashSurplus;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;


    private AccountAdapter mAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_menu_my;
    }


    @Override
    protected void initEventAndData() {
        //改变加载显示的颜色
        swipe.setColorSchemeColors(getResources().getColor(R.color.text_red), getResources().getColor(R.color.text_red));
        //设置向下拉多少出现刷新
        swipe.setDistanceToTriggerSync(800);
        //设置刷新出现的位置
        swipe.setProgressViewEndTarget(false, 200);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                setTestData();
                swipe.setRefreshing(false);
            }
        });

        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new AccountAdapter();
        rvList.setAdapter(mAdapter);
        setTestData();
    }


    private void setTestData() {
        //获取账单列表
        List<BillBean> billList = CacheUtils.getBillList();
        //获取账户列表
        List<AccountBean> bankList = CacheUtils.getAccountList();
        if (billList!=null&&!billList.isEmpty()) {
            //给账户列表的每一个账户的收入/支出赋值
            for (AccountBean accountBean : bankList) {
                //收入
                double income = 0;
                //支出
                double outcome = 0;
                for (BillBean bean : billList) {
                    //先判断是不是银行卡
                    if (TextUtils.equals("银行卡",accountBean.accountType)) {
                        String bankStr = "";
                        //如果是银行卡,先判断银行卡号是否一样
                            if (accountBean.bankNo.length() > 4) {
                                bankStr="银行卡(" + accountBean.bankNo.substring(accountBean.bankNo.length() - 4) + ")";
                            } else {
                                bankStr="银行卡(" + accountBean.bankNo + ")";
                            }
                        if (TextUtils.equals(bankStr,bean.account)) {
                            if (TextUtils.equals("1", bean.billType)) {
                                //支出
                                outcome += NumberUtils.string2Double(bean.money);
                            } else {
                                //收入
                                income += NumberUtils.string2Double(bean.money);
                            }
                        }
                    }else{
                        if (TextUtils.equals(accountBean.accountType,bean.account)) {
                            if (TextUtils.equals("1", bean.billType)) {
                                //支出
                                outcome +=NumberUtils.string2Double(bean.money);
                            } else {
                                //收入
                                income += NumberUtils.string2Double(bean.money);
                            }
                        }
                    }

                }
                accountBean.income = ""+income;
                accountBean.outcome = ""+outcome;
            }
        }

        if (mAdapter != null) {
            mAdapter.setList(bankList);
        }
        //所有账户的总收入
        double totalIncome = 0;
        //所有账户的总支出
        double totalOutcome = 0;
        for (AccountBean accountBean : bankList) {
            if (accountBean != null) {
                totalIncome +=   NumberUtils.string2Double(accountBean.income);
                totalOutcome +=  NumberUtils.string2Double(accountBean.outcome);
            }
        }
        //计算所有账户的总结余并保留两位小数
        cashSurplus.setText(String.format("%.2f", NumberUtils.sub(""+totalIncome,""+totalOutcome)));
    }

    @OnClick({R.id.tv_add})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                startActivity(new Intent(getActivity(), AddBankActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setTestData();
    }
}
